package com.czy.owner.ui.garage;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.BaseRecyclerAdapter;
import com.czy.owner.adapter.CarKeyboardAdapter;
import com.czy.owner.adapter.ProvinceCharAdapter;
import com.czy.owner.adapter.ProvinceCodeAdapter;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.entity.ShortHandsModel;
import com.czy.owner.global.Constants;
import com.czy.owner.db.UserHelper;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wangyang on 2017/1/7 0007.
 * 车牌通用选择
 */

public class CarUtils {
    private static List<ShortHandsModel> shortHandsModelList = new ArrayList<>();
    private static List<String> cityProvince = new ArrayList<>();
    public static void shortHands(final Activity activity) {
        String url = Constants.APP_API_BASE + Constants.PUB_SHORTHANDS;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(activity).getSession());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    String data = PhoneUtils.checkResponseFlag(activity, result);
                    if (data != null) {
                        shortHandsModelList.clear();
                        shortHandsModelList = new Gson().fromJson(data, new TypeToken<List<ShortHandsModel>>() {
                        }.getType());
                        cityProvince.clear();
                        for (int i = 0; i < shortHandsModelList.size(); i++) {
                            cityProvince.add(shortHandsModelList.get(i).getShorthand());
                        }
                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    //车牌缩写
    public static void showProvinceDialog(final Activity activity, final TextView tvProvinceCode, final String strProvinceCode, final TextView tvProviceShortChar, final String strProviceShortChar) {
        // 定义Dialog布局和参数
        final Dialog dialog = new Dialog(activity, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_province_code, null);
        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());
        // 获取自定义Dialog布局中的控件
        RecyclerView rvProvinceCode = (RecyclerView) view.findViewById(R.id.rv_province_code);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(activity, 7);
        rvProvinceCode.setLayoutManager(layoutManager);
        ProvinceCodeAdapter mAdapter = new ProvinceCodeAdapter(activity, cityProvince, strProvinceCode);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object o, View view) {
                tvProvinceCode.setText((String) o);
                if (dialog != null) {
                    dialog.dismiss();
                }
                showProvinceCode(activity, tvProviceShortChar, strProviceShortChar);

            }
        });
        rvProvinceCode.setAdapter(mAdapter);
        RecycleViewDivider mDivider = new RecycleViewDivider(activity, 3, R.drawable.shape_recyclerview_divider_decoration);
        mDivider.setSpanCount(7);
        rvProvinceCode.addItemDecoration(mDivider);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    //车牌字母
    public static void showProvinceCode(Activity activity, final TextView tvProviceShortChar, String strProviceShortChar) {
        // 定义Dialog布局和参数
        final Dialog dialog = new Dialog(activity, R.style.ActionSheetDialogStyle);
        List<String> listCode = new ArrayList<>();
        for (char i = 'A'; i <= 'Z'; i++) {
            listCode.add(i + "");
        }
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_province_code, null);
        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());
        // 获取自定义Dialog布局中的控件
        RecyclerView rvProvinceCode = (RecyclerView) view.findViewById(R.id.rv_province_code);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(activity, 7);
        rvProvinceCode.setLayoutManager(layoutManager);
        ProvinceCharAdapter mAdapter = new ProvinceCharAdapter(activity, listCode, strProviceShortChar);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object o, View view) {
                tvProviceShortChar.setText((String) o);
                if (dialog != null) {
                    dialog.dismiss();
                }


            }
        });
        rvProvinceCode.setAdapter(mAdapter);
        RecycleViewDivider mDivider = new RecycleViewDivider(activity, 3, R.drawable.shape_recyclerview_divider_decoration);
        mDivider.setSpanCount(7);
        rvProvinceCode.addItemDecoration(mDivider);

        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    //数字键盘
    public static void ShowNumber(Activity activity, EasyRecyclerView rvProvinceCode, final EditText etCarCode) {
        CarKeyboardAdapter adapter = null;
        List<String> listCode = new ArrayList<>();
        listCode.add("0");
        listCode.add("1");
        listCode.add("2");
        listCode.add("3");
        listCode.add("4");
        listCode.add("5");
        listCode.add("6");
        listCode.add("7");
        listCode.add("8");
        listCode.add("9");
        rvProvinceCode.setLayoutManager(new GridLayoutManager(activity, 10));
        rvProvinceCode.setAdapterWithProgress(adapter = new CarKeyboardAdapter(activity));
        adapter.addAll(listCode);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<String>() {
            @Override
            public void onItemClick(View view,int position, String o) {
                etCarCode.setText(etCarCode.getText() + (String) o);
            }
        });

    }

    public static void etChangeListener(final EditText etCarCode, final EasyRecyclerView rvProvinceCode) {
        etCarCode.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {//获得焦点
                    rvProvinceCode.setVisibility(View.VISIBLE);
                } else {//失去焦点
                    rvProvinceCode.setVisibility(View.GONE);
                }
            }
        });

        etCarCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                etCarCode.removeTextChangedListener(this);
                etCarCode.setText(s.toString().toUpperCase());
                etCarCode.setSelection(s.toString().length());
                etCarCode.addTextChangedListener(this);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



    }


}